import { imgurl,urls } from '@/utils/constant.js';
import { getservicelist, getcategory } from '@/api/api.js';
export default {
		data() {
			return {
				imgurl,
				current: undefined,
				tabs: [],
				items: [],
				id: undefined,
			}
		},
		watch: {
			current(v) {
				const cid = this.tabs[v].id;
				cid && this.getservicelist(cid);
			},
			items(v) {
				if (v.length) {
					this.id = v[0].id;
				}
			}
		},
		computed: {
			banner() {
				if (this.tabs[this.current]) {
					return this.tabs[this.current]['banner'];
				}
			},
		},
		onLoad(props) {
			console.log('props: %o', props)
			this.getcategory(props.pid, props.cid);
		},
		methods: {
			async getcategory(pid, cid) {
				const ret = await getcategory(pid);
				if (ret && ret.rows) {
					this.tabs = ret.rows.map((row) => {
						let imgs = [];
						if (row.image) {
							imgs = row.image.split(',').map(imgPath => urls.requestURL + '/' + imgPath)
						}
						return {
							...row,
							banner: urls.requestURL + '/' + row.banner,
							label: row.name,
							image: {
								active: imgs[0],
								normal: imgs[1]
							}
						}
					});
					if (cid) {
						let idx = this.tabs.findIndex(item => item.id === +cid);
						if (~idx) {
							this.current = idx;
						}
					} else {
							this.current = 0;
					}
				}
			},
 			async getservicelist(cid) {
				this.items = [];
				const ret = await getservicelist(cid);
				if (ret && ret.rows) {
					this.items = ret.rows.map(row => {
						let tags;
						let specification;
						try {
							tags = JSON.parse(row.list_hotjson);
						}catch(err) {}
						try {
							specification = JSON.parse(row.specificationsjson)
						}catch(err) {}
						return {
							id: row.id,
							thumbimage: urls.requestURL + '/' + row.thumbimage,
							name: row.name,
							price: row.price,
							tags,
							specification,
						}
					})
				}
			}
		},
		}